package com.ufaud.ais.sys.webapp.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ufaud.ais.pub.webapp.action.BaseAction;
import com.ufaud.ais.sys.entity.Department;
import com.ufaud.ais.sys.entity.Employee;
import com.ufaud.ais.sys.service.IDepartmentService;
import com.ufaud.ais.sys.service.IEmployeeService;

public class DeptAction extends BaseAction {

	private static Log LOG = LogFactory.getLog(DeptAction.class);

	private Long id;
	private Department dept;
	private List<Department> deptList;
	private List<Employee> employeeList;
	private IDepartmentService deptService;
	private IEmployeeService employeeService;

//	public String list() {
//		return SUCCESS;
//	}
//	
//	public String tree() {
//		return SUCCESS;
//	}

	public String edit() {
		LOG.debug("id=" + id);
		dept = deptService.getWithSuperDeptAndDeptManager(id);
		deptList = deptService.getAncestorAndSiblingDeptById(id);
		employeeList = employeeService.getAll();
		return SUCCESS;
	}

	public String create() {
		deptList = deptService.getAll();
		employeeList = employeeService.getAll();
		return SUCCESS;
	}

	public String view() {
		dept = deptService.getWithSuperDeptAndDeptManager(id);
		return SUCCESS;
	}

	public String doInsert() {
		if(dept.getDeptManager().getEmployeeId().equals(-1L)) {
			dept.setDeptManager(null);
		}
		if(dept.getSuperDept().getDeptId().equals(-1L)) {
			dept.setSuperDept(null);
		}
		try {
			deptService.save(dept);
			Long newId = dept.getDeptId();
			LOG.debug("dept inserted, id=" + newId);
			addJsonObjectProperty("success", "success");
			addJsonObjectProperty("id", newId + "");
		} catch (Exception e) {
			LOG.error(e);
			addJsonObjectProperty("exception", e.getMessage());
		}
		return SUCCESS;
	}

	public String doUpdate() {
		if(dept.getDeptManager().getEmployeeId().equals(-1L)) {
			dept.setDeptManager(null);
		}
		if(dept.getSuperDept().getDeptId().equals(-1L)) {
			dept.setSuperDept(null);
		}
		try {
			deptService.updateDepartment(dept);
			addJsonObjectProperty("id", dept.getDeptId() + "");
			addJsonObjectProperty("success", "success");
		} catch (Exception e) {
			LOG.error(e);
			addJsonObjectProperty("exception", e.getMessage());
		}
		return SUCCESS;
	}

	public String doDelete() {
		LOG.debug("id=" + id);
		try {
			deptService.deleteDepartmentById(id);
			addJsonObjectProperty("success", "success");
		} catch (Exception e) {
			LOG.error(e);
			addJsonObjectProperty("exception", e.getMessage());
		}
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setDeptService(IDepartmentService deptService) {
		this.deptService = deptService;
	}

	public Department getDept() {
		return dept;
	}

	public void setDept(Department dept) {
		this.dept = dept;
	}

	public List<Department> getDeptList() {
		return deptList;
	}

	public void setDeptList(List<Department> deptList) {
		this.deptList = deptList;
	}

	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}

	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

}
